package com.wanghaixin.controller;

import com.wanghaixin.dto.DataGridResult;
import com.wanghaixin.dto.QueryDTO;
import com.wanghaixin.pojo.Message;
import com.wanghaixin.service.MessageService;
import com.wanghaixin.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Auther: 王海新
 * @Date: 2020/11/3 15:38
 * @Description: 消息模块
 */
@Controller
public class MessageController {

    @Autowired
    private MessageService messageService;

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  15:40  2020/11/3
     * @Version:  1.0.0
     * @Description:  分页查询
     */
    @RequestMapping("/sys/message/list")
    //不添加下面注解会报一下错误
    //Error resolving template [sys/make/list], template might not exist or might not be accessible by any of the configured Template Resolvers
    @ResponseBody
    public DataGridResult findByPage(QueryDTO queryDTO){
        return messageService.findByPage(queryDTO);
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  15:40  2020/11/3
     * @Version:  1.0.0
     * @Description:  删除
     */
    @RequestMapping("/sys/message/del")
    @ResponseBody
    public R delMessage(@RequestBody List<Long> ids){
        for (Long id : ids) {
            messageService.delMessage(id);
        }
        return R.ok();
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  15:40  2020/11/3
     * @Version:  1.0.0
     * @Description:  id查询
     */
    @RequestMapping("/sys/message/info/{id}")
    @ResponseBody
    public R findById(@PathVariable("id") Long id){
        Message message = messageService.findById(id);
        return R.ok().put("message",message);
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:   15:40  2020/11/3
     * @Version:  1.0.0
     * @Description:  添加方法
     */
    @RequestMapping("/sys/message/save")
    @ResponseBody
    public R addMessage(@RequestBody Message message){
        int i = messageService.addMessage(message);
        return i>0 ? R.ok():R.error("添加失败");
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  15:40  2020/11/3
     * @Version:  1.0.0
     * @Description:  修改方法
     */
    @RequestMapping("/sys/message/update")
    @ResponseBody
    public R upMessage(@RequestBody Message message){
        int i = messageService.updateMessage(message);
        return i>0 ? R.ok():R.error("修改失败");
    }
}
